package gervosou;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JSeparator;
import java.awt.Panel;
import java.awt.TextArea;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.JList;

public class VueNouvCompteSociete extends Vue {

	private Societe _societe ;
	
	private JPanel contentPane;
	private JTextField textFieldNumero;
	private JTextField textFieldSolde;
	private JTextField textFieldAdresse;
	private JTextField textFieldNbComptes;
	private JTextArea textAreaInfosComptes;
	
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JButton btnRechercher;
	private JButton btnAnnuler;
	private JButton btnEnregistrer;
	
	/**
	 * Create the frame.
	 */
	public VueNouvCompteSociete(Controleur controleur) {
		super(controleur);
		setTitle("Enregistrement d'un nouveau compte pour un client existant");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 618, 455);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(26, 61, 302, 115);
		contentPane.add(scrollPane);
		
		final JList listSoc = new JList();
		scrollPane.setColumnHeaderView(listSoc);
		listSoc.setListData(getControleur().getSociete().keySet().toArray());


		
		
		JLabel lblSoc = new JLabel("Societes : ");
		lblSoc.setBounds(6, 12, 81, 16);
		contentPane.add(lblSoc);
		
		JLabel lblAdresse = new JLabel("Adresse :");
		lblAdresse.setBounds(26, 193, 61, 16);
		contentPane.add(lblAdresse);
		
		textFieldAdresse = new JTextField();
		textFieldAdresse.setColumns(10);
		textFieldAdresse.setBounds(99, 188, 134, 28);
		contentPane.add(textFieldAdresse);
		
		JLabel lblNbComptes = new JLabel("Nb de comptes :");
		lblNbComptes.setBounds(251, 193, 104, 16);
		contentPane.add(lblNbComptes);
		
		textFieldNbComptes = new JTextField();
		textFieldNbComptes.setColumns(10);
		textFieldNbComptes.setBounds(354, 188, 134, 28);
		contentPane.add(textFieldNbComptes);
		
		
		btnRechercher = new JButton("Rechercher");
		btnRechercher.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				String nom = listSoc.getSelectedValue().toString();
				// liaison de la vue avec l'objet observe
				_societe = getControleur().getSociete(nom);
				if (_societe != null){
					alimente(_societe);
					
				}
			}
			});
		
		btnRechercher.setBounds(335, 142, 117, 29);
		contentPane.add(btnRechercher);
		
		btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					getControleur().fermerVue(VueNouvCompteSociete.this);}
		});
		btnAnnuler.setBounds(464, 142, 117, 29);
		contentPane.add(btnAnnuler);
		
		JScrollPane scrollPane1 = new JScrollPane();
		scrollPane1.setBounds(26, 228, 462, 47);
		contentPane.add(scrollPane1);
		
		textAreaInfosComptes = new JTextArea();
		textAreaInfosComptes.setEditable(false);
		scrollPane1.setColumnHeaderView(textAreaInfosComptes);

		JLabel lblCompte = new JLabel("Compte :");
		lblCompte.setBounds(6, 301, 61, 16);
		contentPane.add(lblCompte);
		
		JLabel lblNumero = new JLabel("Num\u00E9ro g\u00E9n\u00E9r\u00E9 :");
		lblNumero.setBounds(352, 336, 104, 16);
		contentPane.add(lblNumero);
		
		textFieldNumero = new JTextField();
		textFieldNumero.setColumns(10);
		textFieldNumero.setBounds(474, 331, 107, 28);
		contentPane.add(textFieldNumero);
		
		JLabel lblType = new JLabel("Type :");
		lblType.setBounds(26, 320, 61, 16);
		contentPane.add(lblType);
		
		final JRadioButton rdbtnCourant = new JRadioButton("Courant");
		buttonGroup.add(rdbtnCourant);
		rdbtnCourant.setBounds(83, 317, 104, 23);
		contentPane.add(rdbtnCourant);
		
		final JRadioButton rdbtnCEL = new JRadioButton("CEL");
		buttonGroup.add(rdbtnCEL);
		rdbtnCEL.setBounds(191, 317, 79, 23);
		contentPane.add(rdbtnCEL);
		
		final JRadioButton rdbtnPEL = new JRadioButton("PEL");
		buttonGroup.add(rdbtnPEL);
		rdbtnPEL.setBounds(268, 317, 141, 23);
		contentPane.add(rdbtnPEL);
		
		JLabel lblSoldeInitial = new JLabel("Solde initial :");
		lblSoldeInitial.setBounds(68, 363, 95, 16);
		contentPane.add(lblSoldeInitial);
		
		textFieldSolde = new JTextField();
		textFieldSolde.setColumns(10);
		textFieldSolde.setBounds(166, 358, 134, 28);
		contentPane.add(textFieldSolde);
		
		btnEnregistrer = new JButton("Enregistrer");
		btnEnregistrer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String s = textFieldSolde.getText();
				String type;
				if (rdbtnCourant.isSelected()) {
					type = "Courant";}
				else if (rdbtnCEL.isSelected()) {
					type = "CEL";}
				else {
					type = "PEL";}
				getControleur().nouvCompteSociete(type, s, _societe);
				}
		});
		btnEnregistrer.setBounds(464, 377, 117, 29);
		contentPane.add(btnEnregistrer);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(26, 287, 572, 2);
		contentPane.add(separator);
	}

	/**
	 * definition des etats de la fenetre
	 */
	public void setEtat (int etat){
		switch (etat) {
		case initiale: {
			textFieldAdresse.setEnabled(false);
			textFieldNbComptes.setEnabled(false);
			textFieldSolde.setEnabled(false);

			textFieldNumero.setEnabled(false);
			btnEnregistrer.setVisible(true);
			break;
			}
		case finale: {
			btnRechercher.setEnabled(false);
			textFieldAdresse.setEditable(false);
			textFieldNbComptes.setEditable(false);

			textFieldSolde.setEnabled(true);
			textFieldNumero.setEditable(false);
			btnEnregistrer.setVisible(true);
			btnEnregistrer.setEnabled(true);
			break;
			}
		}
	}
	
	private Societe getSociete() {
		return _societe;
	}
	private void setSociete(Societe s) {
		 _societe = s;
	}
	
	public void alimente(Societe s) {
		textFieldAdresse.setText(s.getAdresse());
		textFieldNbComptes.setText(String.valueOf(s.getNbComptes()));
		textAreaInfosComptes.setText("");
		for (CompteSociete cs : s.getComptes()) {
			  textAreaInfosComptes.append( "num�ro " + String.valueOf(cs.getNumero()) + " solde " + String.valueOf(cs.getSolde()) + "\n");
		}
		this.setEtat(finale);
	}
	
	public void alimenteNum(int n) {
		textFieldNumero.setText(String.valueOf(n));
	}
}


